#include "table.h"

/**
 * 冒泡排序
 * @param {table} *tab
 * @return {void}
 */
void bubblesort(table *tab)
{
    int i = 1, j, done = 1;

    while (i <= tab->length && done)
    {
        done = 0;
        for (j = 1; j <= tab->length - i; j++)
        {
           if(tab -> r[j + 1].key < tab -> r[j].key)
           {
                tab -> r[0] = tab -> r[j];
                tab -> r[j] = tab -> r[j + 1];
                tab -> r[j + 1] = tab -> r[0];
                done = 1;
           }
        }
        i++;
    }
}

int main()
{
    table tab;
    int i;
    int arr[] = {0, 9, 1, 5, 8, 3, 7, 4, 6, 2};
    tab.length = 0;

     for (i = 1; i <= 9; i++)
    {
        tab.r[i].key = arr[i];
        tab.length++;
    }

    bubblesort(&tab);

    for (i = 1; i <= tab.length; i++)
    {
        printf("%2d ", tab.r[i].key);
    }

    return 0;
}